package cn.jy.nvr.controller;


import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cn.jy.nvr.datasource.DataSourceConst;
import cn.jy.nvr.datasource.DataSourceContextHolder;
import cn.jy.nvr.domain.ShareVideo;
import cn.jy.nvr.mapper.ShareVideoMapper;
import cn.jy.nvr.service.IShareVideoService;
import cn.jy.nvr.utils.AjaxResult;

@Controller
public class ShareVideocontroller {
	
	@Autowired
	ShareVideoMapper mapper;
	
	@Autowired
	IShareVideoService shareVideoService;
	@RequestMapping("shareVideo")
	public String selectShareVideo(){
		return "shareVideo/list";
	}
	
	@RequestMapping("ShareVideo")
	@ResponseBody
	public List<ShareVideo> Collecting( String type){
		if(type==null||type.length()<0){
			return null;
		}
		 DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
		List<ShareVideo> pathByType = mapper.getPathByType(type);
		return  pathByType;
	}
	@RequestMapping("shareVideo/list")
	@ResponseBody
	public List<ShareVideo> list(){
		DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
		List<ShareVideo> shareVideoList = shareVideoService.getAll();
		return  shareVideoList;
	}
	@RequestMapping("shareVideo/del")
	@ResponseBody
	public AjaxResult  del(Integer id){
		DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
		try {
			shareVideoService.del(id);
		} catch (Exception e) {
			e.printStackTrace();
			return new AjaxResult("删除失败"+e.getMessage());
		}
		return new AjaxResult();
		
	}
	@RequestMapping("shareVideo/save")
	@ResponseBody
	public AjaxResult  save(MultipartFile film,MultipartFile filmImage,String filmName,String filmType){
		DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
		try {
			film.getSize();
			String filmPath="D:/apache-tomcat-7.0.79/webapps/Nvr/ShareVideo";
			String filmImagePath="D:/apache-tomcat-7.0.79/webapps/Nvr/ShareVideoImage";
			 String filmFileName = film.getOriginalFilename();
			 String filmageFileName=filmImage.getOriginalFilename();
			File filmFile=new File(filmPath,filmFileName);
			File filmImageFile=new  File(filmImagePath,filmageFileName);
			
			 FileOutputStream fost  = new FileOutputStream(filmFile);
			 FileOutputStream ifost= new FileOutputStream(filmImageFile);
			IOUtils.copy(film.getInputStream(),fost);
			IOUtils.copy(filmImage.getInputStream(),ifost);
			
			film.getInputStream().close();
			filmImage.getInputStream().close();
			fost.close();
			ifost.close();
			ShareVideo shareVideo=new ShareVideo();
			shareVideo.setFilmName(filmName);
			shareVideo.setImagePath("http://192.168.0.98:8081/Nvr/ShareVideoImage/"+filmImage.getOriginalFilename());
			shareVideo.setPATH("http://192.168.0.98:8081/Nvr/ShareVideo/"+film.getOriginalFilename());
			shareVideo.setTYPE(filmType);
			shareVideoService.add(shareVideo);
			
			
		} catch (Exception e) {
			e.printStackTrace();
			return new AjaxResult("上传失败"+e.getMessage());
		}
		return new AjaxResult();
		
	}
}
